# -*- coding: utf-8 -*-
"""
Created on Wed Mar 27 14:16:54 2024

@author: heyuesong
"""

import requests
import pandas as pd
from utils import general
import execjs
from lxml import etree

# js = general.read_js(r'D:\code\猿人学\第11题.js')


result_list = []

headers = {
    "Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.7",
    "Accept-Language": "zh-CN,zh;q=0.9",
    "Cache-Control": "no-cache",
    "Connection": "keep-alive",
    "Pragma": "no-cache",
    "Referer": "https://www.python-spider.com/challenge/11",
    "Sec-Fetch-Dest": "document",
    "Sec-Fetch-Mode": "navigate",
    "Sec-Fetch-Site": "same-origin",
    "Upgrade-Insecure-Requests": "1",
    "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/122.0.0.0 Safari/537.36",
    "sec-ch-ua": "\"Chromium\";v=\"122\", \"Not(A:Brand\";v=\"24\", \"Google Chrome\";v=\"122\"",
    "sec-ch-ua-mobile": "?0",
    "sec-ch-ua-platform": "\"Windows\""
}
cookies = {
    "sessionid": "s9dhbpkoyuke3k1fxq9uu6953q4c11kl",
    "Hm_lvt_337e99a01a907a08d00bed4a1a52e35d": "1711520242,1711520884",
    "Hm_lpvt_337e99a01a907a08d00bed4a1a52e35d": "1711521374",
    "__jsl_clearance": "1711522872.387|0|clD4VpfqhdaLBWywKWy%2FZyfi6d_a1fb51002769bd09409113e96cbf855a3D"
}
url = "https://www.python-spider.com/challenge/11"

response = requests.get(url, headers=headers, cookies=cookies)

jss = general.requests_re('(var[\s\S]*)</script>', response.text)

with open(r'D:\code\猿人学\第11题.js','r') as f:
    js = f.read().replace('js_code',jss)

data = execjs.compile(js).call('get_cookie')


c = general.requests_re('__jsl_clearance=(.*?);Expire', data)
cookies['__jsl_clearance'] = c

response = requests.get(url, headers=headers, cookies=cookies)
html = etree.HTML(response.text)
l = sum([int(x.replace('\t','').replace('\n','')) for x in html.xpath('//td[@class="info"]/text()')])
